package com.jxt.myjxtmanager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jxt.myjxtmanager.ui.fragment.HomeFragment;

/**
 * Created by Administrator on 2016/1/15 0015.
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    protected View mView;
    private TextView tvTitle;
    private Toolbar toolbar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mView = inflater.inflate(getLayoutId(), null);
        initView();
        return mView;
    }


    /**
     * layout xml文件ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 控件初始化
     */
    protected void initView() {
    }

    public static BaseFragment newInstance(Class<HomeFragment> clazz) {
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        throw new RuntimeException("Fragment 实例化失败！");

    };

    @Override
    public void onClick(View v) {
        onViewClick(v.getId());
    }


    /**
     * 左边按键点击事件
     */
    public void onClickLeftMenu() {
    }


    /**
     * 控件点击事件
     *
     * @param key
     */
    protected void onViewClick(int key) {
    }


    @SuppressWarnings("unchecked")
    protected <T extends View> T getViewById(View view, int id) {
        return (T) view.findViewById(id);
    }

    @SuppressWarnings("unchecked")
    protected <T extends View> T getView(int id) {
        return (T) mView.findViewById(id);
    }


    private Toolbar getToolbar() {
        if (toolbar == null) {
            toolbar = (Toolbar)mView.findViewById(R.id.toolbar);
            toolbar.setTitle("");
            ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        } //(Toolbar) mView.findViewById(R.id.toolbar);
        return toolbar;
    }

    /**
     * 添加toolbar标题
     */
    public void setToolbarTitle(int resText) {
        setToolbarTitle(getString(resText));
    }


    /**
     * 去除 ToolbarNavigationIcon
     */
    public void setToolbarNavigationIcon() {
        Toolbar toolbar = getToolbar();
        if (toolbar != null) {
            ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
            getToolbar().setNavigationIcon(null);
        }
    }

    /**
     * 自定义 ToolbarNavigationIcon
     */
    public void setToolbarNavigationIcon(int id) {
        Toolbar toolbar = getToolbar();
        if (toolbar != null) {
            ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
            getToolbar().setNavigationIcon(id);
            getToolbar().setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickLeftMenu();
                }
            });
        }
    }

    /**
     * 添加toolbar标题
     */
    public void setToolbarTitle(String textTitle) {
        Toolbar toolbar = getToolbar();
        if (toolbar != null) {
            getToolbar().setTitle("");
            if (tvTitle == null) {
                tvTitle = (TextView)mView.findViewById(R.id.app_title_text);
            }
            tvTitle.setText(textTitle);
            //((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        }
    }

    /**
     * 隐藏toolBar
     */
    protected void hideTitle() {
        if (getToolbar() == null) {
            return;
        }
        getToolbar().setVisibility(View.GONE);
    }
}
